<template>
  <div id="box">
    <el-form ref="form" status-icon :model="form" :rules="rules" label-width="80px" v-loading="loading">
      <el-form-item label="用户名" prop="username">
        <el-input v-model="form.username"></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input v-model="form.password" type="password"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('form')">登录</el-button>
        <el-button @click="resetForm('form')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
  export default {
    name: 'login',
    data() {
      var validatePass = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请输入密码'));
        } else {
          callback();
        }
      };
      var validatenName = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请输入用户名'));
        }else {
          callback();
        }
      };
      return {
        form: {
          username: '',
          password: '',
        },
        rules: {
          username: [
            { validator: validatenName, trigger: 'blur' }
          ],
          password: [
            { validator: validatePass, trigger: 'blur' }
          ],
        },
        loading: false
      };
    },
    
    methods: {
      submitForm(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            this.loading = true
            setTimeout(() => {
              this.loading = false
            }, 2000);
             this.$router.push({path:'/user'});
          } else {
            console.log('error submit!!');
            return false;
          }
        });
      },
      resetForm(formName) {
        this.$refs[formName].resetFields();
      }
    }
  }
</script>

<style scoped>
  #box{
    text-align: center;
    width: 500px;
    height: auto;
    border: 1px solid blcak;
    background: #ccffff;
    margin: 100px auto;
    padding: 100px;
    box-shadow: 10px 10px 5px #888888;
  }
</style>

